using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using SubSonic;
using SubSonic.Utilities;


namespace OpenProject.DataObjects
{
    /// <summary>
    /// Controller class for Phase
    /// </summary>
    [System.ComponentModel.DataObject]
    public partial class PhaseController
    {
        // Preload our schema..
        Phase thisSchemaLoad = new Phase();
        private string userName = string.Empty;
        protected string UserName
        {
            get
            {
				if (userName.Length == 0) 
				{
    				if (System.Web.HttpContext.Current != null)
    				{
						userName=System.Web.HttpContext.Current.User.Identity.Name;
					}

					else
					{
						userName=System.Threading.Thread.CurrentPrincipal.Identity.Name;
					}

				}

				return userName;
            }

        }

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public static PhaseCollection FetchAll()
        {
            PhaseCollection coll = new PhaseCollection();
            Query qry = new Query(Phase.Schema);
            coll.LoadAndCloseReader(qry.ExecuteReader());
            return coll;
        }

        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static PhaseCollection FetchByID(object PhaseId)
        {
            PhaseCollection coll = new PhaseCollection().Where("PhaseId", PhaseId).Load();
            return coll;
        }

		
		[DataObjectMethod(DataObjectMethodType.Select, false)]
        public static PhaseCollection FetchByQuery(Query qry)
        {
            PhaseCollection coll = new PhaseCollection();
            coll.LoadAndCloseReader(qry.ExecuteReader()); 
            return coll;
        }

        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public static bool Delete(object PhaseId)
        {
            return (Phase.Delete(PhaseId) == 1);
        }

        [DataObjectMethod(DataObjectMethodType.Delete, false)]
        public static bool Destroy(object PhaseId)
        {
            return (Phase.Destroy(PhaseId) == 1);
        }

        
        
        
        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public static bool Delete(int PhaseId)
        {
            Query qry = new Query(Phase.Schema);
            qry.QueryType = QueryType.Delete;
            qry.AddWhere("PhaseId", PhaseId);
            qry.Execute();
            return (true);
        }
        
       
    	
    	
	    /// <summary>
	    /// Inserts a record, can be used with the Object Data Source
	    /// </summary>
        [DataObjectMethod(DataObjectMethodType.Insert, true)]
	    public static void Insert(string Title,DateTime? StartDate,DateTime? EndDate,int? ProjectId)
	    {
		    Phase item = new Phase();
		    
            item.Title = Title;
            
            item.StartDate = StartDate;
            
            item.EndDate = EndDate;
            
            item.ProjectId = ProjectId;
            
	    
		    item.Save(UserName);
	    }

    	
	    /// <summary>
	    /// Updates a record, can be used with the Object Data Source
	    /// </summary>
        [DataObjectMethod(DataObjectMethodType.Update, true)]
	    public static void Update(int PhaseId,string Title,DateTime? StartDate,DateTime? EndDate,int? ProjectId)
	    {
		    Phase item = new Phase();
		    
				item.PhaseId = PhaseId;
				
				item.Title = Title;
				
				item.StartDate = StartDate;
				
				item.EndDate = EndDate;
				
				item.ProjectId = ProjectId;
				
		    item.MarkOld();
		    item.Save(UserName);
	    }

    }

}

